package cn.itcast.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.springframework.stereotype.Component;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class WeatherUtils {

    private static String SERVICES_HOST = "www.webxml.com.cn";
    private static String WEATHER_SERVICES_URL = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/";
    // 获得本天气预报Web Services支持的洲、国内外省份和城市信息
    private static String SUPPORT_CITY_URL = WEATHER_SERVICES_URL
            + "getSupportCity?byProvinceName=";
    // 根据城市或地区名称查询
    private static String WEATHER_QUERY_URL = WEATHER_SERVICES_URL
            + "getWeatherbyCityName?theCityName=";

    /**
     * 将文件转换为流
     *
     * @param url
     * @return
     * @throws IOException
     */
    public static InputStream getSoapInputStream(String url) throws IOException {
        InputStream is = null;
        URL U = new URL(url);
        URLConnection conn = U.openConnection();
        conn.setRequestProperty("Host", SERVICES_HOST);
        conn.connect();
        is = conn.getInputStream();
        return is;
    }

    // 取得支持的城市列表
    public static ArrayList<String> getSupportCity(String provinceName)
            throws ParserConfigurationException, IOException, SAXException {
        ArrayList cityList = null;
        Document doc;
        // 首先得到：得到 DOM 解析器的工厂实例
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        // 如果生成的解析器将提供对 xml 名称空间的支持，则为 true；否则为 false
        dbf.setNamespaceAware(true);
        // 然后从 DOM 工厂获得 DOM 解析器
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputStream is = getSoapInputStream (SUPPORT_CITY_URL + provinceName);
        doc = db.parse(is);
        NodeList nl = doc.getElementsByTagName("string");
        int len = nl.getLength();
        cityList = new ArrayList<String>(len);
        for (int i = 0; i < len; i++) {
            Node n = nl.item(i);
            String city = n.getFirstChild().getNodeValue();
            cityList.add(city);
        }
        is.close();
        return cityList;
    }

    // 取得城市的天气
    public static ArrayList<String> getWeather(String city)
            throws ParserConfigurationException, UnsupportedEncodingException,
            IOException, SAXException {
        ArrayList weatherList = null;

        // 首先得到：得到 DOM 解析器的工厂实例
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        // 如果生成的解析器将提供对 xml 名称空间的支持，则为 true；否则为 false
        dbf.setNamespaceAware(true);
        // 然后从 DOM 工厂获得 DOM 解析器
        DocumentBuilder db = dbf.newDocumentBuilder();
        // 将XML文件转成流
        InputStream is = getSoapInputStream(WEATHER_QUERY_URL + city);
        // 解析 XML 文档的输入流，得到一个 Document
        Document doc = db.parse(is);
        // 得到 XML 文档的根节点
        Element root = doc.getDocumentElement();
        // 得到节点的子节点
        NodeList nl = root.getElementsByTagName("string");
        int len = nl.getLength();
        weatherList = new ArrayList<String>(len);
        for (int i = 0; i < len; i++) {
            Node n = nl.item(i);
            String weather = n.getFirstChild().getNodeValue();
            weatherList.add(weather);
        }
        is.close();
        return weatherList;
    }
}
